<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Model\Category;
use App\Model\Goods;
use App\Model\Ads;
use DB,Session,Config,Input,View,Cache;

class IndexController extends CommonController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        // 显示三级分类
        $navlist = Category::getTree();
        // 获取商品列表
        // 获取所有的一级分类
        $indexCates = Category::where('pid', 0)
                ->where('cate_status', 1)
                ->orderBy('id', 'asc')
                ->get();
        
        // 获取当前分类下的所有商品
        $indexGoods = [];
        foreach($indexCates as $cate){
            $indexGoods[$cate->id] = Goods::GoodsList($cate->id, 8);
        }
        
        // 获取广告
        $slide = $this->getAds('slide');
        $cateAds = $this->getAds('cateAds', 1, 5);
        $actAds = $this->getAds('actAds', 2, 6);
        $floorAds = $this->getAds('floorAds', 3, 8);
        return view('home.index.index', compact('navlist', 'indexCates', 'indexGoods','slide','cateAds', 'actAds','floorAds'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        $data = $request->old();
        return view("home.create", compact("data"));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
    
    // 获取广告
    public function getAds($name, $position=0, $limit=6)
    {
        if(Cache::has($name)){
            return Cache::get($name);
        } else {
            $ads = Ads::getAdsByPosition($position,$limit);
            Cache::put($name, $ads, 10);
        }
        return $ads;
    }
}
